package com.zzyl.controller;

import com.aliyun.iot20180120.models.QueryDevicePropertyStatusResponseBody;
import com.aliyun.iot20180120.models.QueryThingModelPublishedResponseBody;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDto;
import com.zzyl.entity.DeviceData;
import com.zzyl.mapper.DeviceMapper;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceDataVo;
import com.zzyl.vo.DevicePorpertyStatusInfoVo;
import com.zzyl.vo.DeviceVo;
import com.zzyl.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description DeviceController
 * @Author loe
 * @Date 2024-07-23
 */

@RestController
@RequestMapping("/iot")
@Api(tags = "iot设备管理")
public class DeviceController extends BaseController{

    @Autowired
    private DeviceService deviceService;

    /**
     * 从物联网平台同步产品列表
     * @return
     */
    @PostMapping("/syncProductList")
    @ApiOperation("从物联网平台同步产品列表")
    public ResponseResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation("查询所有产品列表")
    public ResponseResult<List<ProductVo>> gerAllProductList() {
        List<ProductVo> productVoList = deviceService.gerAllProductList();
        return success(productVoList);
    }

    /**
     * 注册设备
     * @return
     */
    @PostMapping("/RegisterDevice")
    @ApiOperation("注册设备")
    public ResponseResult registerDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.registerDevice(deviceDto);
        return success();
    }

    /**
     * 分页查询设备列表
     * @param pageNum
     * @param pageSize
     * @param deviceName
     * @param locationType
     * @param productKey
     * @return
     */
    @GetMapping("/pageQueryDevice")
    @ApiOperation("分页查询设备列表")
    public ResponseResult<PageResponse<DeviceVo>> pageQueryDevice(
           @ApiParam(value = "页码") Integer pageNum,
           @ApiParam(value = "页面大小") Integer pageSize,
           @ApiParam(value = "设备名称") String deviceName,
           @ApiParam(value = "位置类型 0 随身设备 1固定设备") Integer locationType,
           @ApiParam(value = "产品的ProductKey,物联网平台产品唯一标识") String productKey
    ) {
        PageResponse<DeviceVo> deviceVoList = deviceService.pageQueryDevice(pageNum, pageSize, deviceName, locationType, productKey);
        return success(deviceVoList);
    }

    /**
     * 查询设备详细数据
     * @param iotId
     * @param productKey
     * @return
     */
    @PostMapping("/QueryDeviceDetail")
    @ApiOperation("查询设备详细数据")
    public ResponseResult<DeviceVo> QueryDeviceDetail(
           @RequestBody DeviceDto deviceDto
    ) {
        DeviceVo deviceVo = deviceService.QueryDeviceDetail(deviceDto);
        return success(deviceVo);
    }

    /**
     * 查询指定设备的物模型运行状态
     * @param deviceDto
     * @return
     */
    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation("查询指定设备的物模型运行状态")
    public ResponseResult<QueryDevicePropertyStatusResponseBody.QueryDevicePropertyStatusResponseBodyData> QueryDevicePropertyStatus(
            @RequestBody DeviceDto deviceDto
    ) {
        QueryDevicePropertyStatusResponseBody.QueryDevicePropertyStatusResponseBodyData devicePorpertyStatusInfoVo = deviceService.QueryDevicePropertyStatus(deviceDto);
        return success(devicePorpertyStatusInfoVo);
    }

    /**
     * 查看指定产品的已发布物模型中的功能定义详情
     * @param deviceDto
     * @return
     */
    @PostMapping("/QueryThingModelPublished")
    @ApiOperation("查看指定产品的已发布物模型中的功能定义详情")
    public ResponseResult QueryThingModelPublished(
            @RequestBody DeviceDto deviceDto
    ) {
        QueryThingModelPublishedResponseBody.QueryThingModelPublishedResponseBodyData data = deviceService.QueryThingModelPublished(deviceDto);
        return success(data);
    }

    /**
     * 修改设备备注名称
     * @param deviceDto
     * @return
     */
    @PostMapping("/UpdateDevice")
    @ApiOperation("修改设备备注名称")
    public ResponseResult updateDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.updateDevice(deviceDto);
        return success();
    }

    /**
     * 删除设备
     * @param deviceDto
     * @return
     */
    @DeleteMapping("/DeleteDevice")
    @ApiOperation("删除设备")
    public ResponseResult deleteDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.deleteDevice(deviceDto);
        return success();
    }
}
